﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using Entity;

namespace Controller
{
    public class VendorController:ControllerBase
    {

        public IList<VendorEntity> FindAllVendorEntities()
        {
            IList<Vendor> vendors = (from v in Sdc.Vendors
                                       select v).ToList();
            IList<VendorEntity> vendorEntities = new List<VendorEntity>();
            foreach (Vendor vendor in vendors)
            {
                VendorEntity vendorEntity = TransformVendorToVendorEntity(vendor);
                vendorEntities.Add(vendorEntity);
            }
            return vendorEntities;
        }
        public VendorEntity findVendorByVendorId(int vendorId)
        {
            Vendor vendor = (from v in Sdc.Vendors
                             where v.Id == vendorId
                             select v).SingleOrDefault();

           VendorEntity vendorEntity = TransformVendorToVendorEntity(vendor);
            return vendorEntity;

        }
        public bool AddVendor(VendorEntity vendorEntity)
        {
            try
            {
                Vendor vendor = TransformVendorEntityToVendor(vendorEntity);
                Sdc.Vendors.InsertOnSubmit(vendor);
                Sdc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static VendorEntity TransformVendorToVendorEntity(Vendor vendor)
        {
            VendorEntity vendorEntity = new VendorEntity();
            vendorEntity.Address = vendor.Address;
            vendorEntity.ContactPerson = vendor.ContactPerson;
            vendorEntity.Email = vendor.Email;
            vendorEntity.Fax = vendor.Fax;
            vendorEntity.Id = vendor.Id;
            vendorEntity.Name = vendor.Name;
            vendorEntity.Phone = vendor.Phone;
            vendorEntity.Remark = vendor.Remark;
            vendorEntity.ZipCode = vendor.ZipCode;
            vendorEntity.ShortName = vendor.ShortName;
            vendorEntity.Number = vendor.Number;
            vendorEntity.Code = vendor.Code;
            return vendorEntity;
        }
        public static Vendor TransformVendorEntityToVendor(VendorEntity vendorEntity)
        {
            Vendor vendor = new Vendor();
            vendor.Address = vendorEntity.Address;
            vendor.ContactPerson = vendorEntity.ContactPerson;
            vendor.Email = vendorEntity.Email;
            vendor.Fax = vendorEntity.Fax;
            vendor.Id = vendorEntity.Id;
            vendor.Name = vendorEntity.Name;
            vendor.Phone = vendorEntity.Phone;
            vendor.Remark = vendorEntity.Remark;
            vendor.ZipCode = vendorEntity.ZipCode;
            vendor.ShortName = vendorEntity.ShortName;
            vendor.Number = vendorEntity.Number;
            vendor.Code = vendorEntity.Code;
            return vendor;
        }


        public bool DeleteVendorById(int vendorId)
        {
            try
            {
                Vendor vendor = (from v in Sdc.Vendors where v.Id == vendorId select v).SingleOrDefault();
                Sdc.Vendors.DeleteOnSubmit(vendor);
                Sdc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool UpdateVendor(VendorEntity vendorEntity)
        {
            try
            {
                Vendor vendor = (from v in Sdc.Vendors where v.Id == vendorEntity.Id select v).SingleOrDefault();
                vendor.Name = vendorEntity.Name;
                vendor.ShortName = vendorEntity.ShortName;
                vendor.Phone = vendorEntity.Phone;
                vendor.Fax = vendorEntity.Fax;
                vendor.Email = vendorEntity.Email;
                vendor.ContactPerson = vendorEntity.ContactPerson;
                vendor.Address = vendorEntity.Address;
                vendor.ZipCode = vendorEntity.ZipCode;
                vendor.Number = vendorEntity.Number;
                vendor.Code = vendorEntity.Code;
                vendor.Remark = vendorEntity.Remark;

                Sdc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
